import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { ElMessage } from 'element-plus';
const history = createWebHistory();
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/login.vue'),
  },
  {
    path: '/listView',
    name: 'ListView',
    component: () => import('../views/ListView.vue'),
    redirect: '/listView/home',
    children: [
      {
        path: 'home',
        meta: {
          title: '首页',
          requireAuth: true
        },
        component: () => import('../views/home/index.vue'),
      },
      {
        path: 'drag-table',
        meta: {
          title: '访客记录/访客列表',
          requireAuth: true
        },
        component: () => import('../views/table/drag-table.vue'),
      },
      {
        path: 'particulars',
        meta: {
          title: '访客记录/访客列表/访客详情',
          requireAuth: true
        },
        component: () => import('../views/table/particulars.vue'),
      },
      {
        path: 'inline-edit-table',
        meta: {
          title: '访客记录/访客添加修改',
          requireAuth: true
        },
        component: () => import('../views/table/inline-edit-table.vue'),
      },
      {
        path: 'instructions-excel',
        meta: {
          title: '访客记录/负责人操作详情',
          requireAuth: true
        },
        component: () => import('../views/table/instructions-excel.vue'),




      }, 
      {
        path: 'keyboard',
        meta: {
          title: '部门管理/部门列表',
          requireAuth: true
        },
        component: () => import('../views/chart/keyboard.vue'),
      }, 
      {
        path: 'keyboardadd',
        meta: {
          title: '部门管理/部门添加修改',
          requireAuth: true
        },
        component: () => import('../views/chart/keyboardadd.vue'),

      }, 


      


      {
        path: 'adminlist',
        meta: {
          title: '管理员/管理员列表',
          requireAuth: true
        },
        component: () => import('../views/Administrator/adminlist.vue'),
      }, 


      {
        path: 'adminadd',
        meta: {
          title: '管理员/管理员添加',
          requireAuth: true
        },
        component: () => import('../views/Administrator/adminadd.vue'),
      }, 

      {
        path: 'reviewerlist',
        meta: {
          title: '核销人员管理/核销人员列表',
          requireAuth: true
        },
        component: () => import('../views/reviewer/reviewerlist.vue'),
      }, 

      {
        path: 'revieweradd',
        meta: {
          title: '核销人员管理/核销人员添加修改',
          requireAuth: true
        },
        component: () => import('../views/reviewer/revieweradd.vue'),
      }, 


      {
        path: 'reviewerlog',
        meta: {
          title: '核销人员管理/核销人员添加修改',
          requireAuth: true
        },
        component: () => import('../views/reviewer/reviewerlog.vue'),
      }, 



      {
        path: 'newslist',
        meta: {
          title: '校园动态/校园动态列表',
          requireAuth: true
        },
        component: () => import('../views/news/newslist.vue'),
      }, 


      {
        path: 'newsadd',
        meta: {
          title: '校园动态/校园动态添加修改',
          requireAuth: true
        },
        component: () => import('../views/news/newsadd.vue'),
      }, 

      {
        path: 'export-excel',
        meta: {
          title: '审核日志/审核列表',
          requireAuth: true
        },
        component: () => import('../views/journal/export-excel.vue'),
      },
      {
        path: 'details',
        meta: {
          title: '审核日志/审核列表/审核详情',
          requireAuth: true
        },
        component: () => import('../views/journal/details.vue'),
      },
      {
        path: 'settings',
        meta: {
          title: '设置',
          requireAuth: true
        },
        component: () => import('../views/settings/settings.vue'),
      },
    ],
  },
];

const router = createRouter({
  history,
  routes,
});


/**
 * 全局前置路由守卫，每一次路由跳转前都进入这个 beforeEach 函数
 */
 router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token') || ''
  // 判断本地缓存有没有token ，如果有token，跳转对应的路由
  if (to.meta.requireAuth) {
    if (token) {
      next()
    } else {
      ElMessage({
        message: '请先登录',
        type: "error",
      });
      next(`/login?redirect=${to.fullPath}`)//同上
    }
  } else {
    next()
  }
})


export default router;
